package cn.dulian.connection;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author dulian
 * @create 2022-04-05 0:17
 */
public class ConnectionTest {
    /*
    方式5：将数据库连接需要的4个基本信息声明在配置文件中，通过读取配置文件的方式，，获取连接
    此种方式的好处？
    1.实现了数据与代码的分离。实现了解耦
    2.如果需要修改配置文件信息，可以避免程序重新打包
     */
    @Test
    public void getConnection5(){
        //1.读取配置文件中的4个基本信息
        InputStream is = null;
        Properties pros = null;
        Connection conn =null;
        try {
            is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
            pros = new Properties();
            pros.load(is);


            String user = pros.getProperty("user");
            String passsword = pros.getProperty("password");
            String url = pros.getProperty("url");
            String driverClass = pros.getProperty("driverClass");

            //2.加载驱动
            Class.forName(driverClass);
            //3.获取连接
            conn = DriverManager.getConnection(url, user, passsword);
            System.out.println("conn = " + conn);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

        }
    }



    @Test
    public void getConnection2(){
        //1.读取配置文件中的4个基本信息
        InputStream is = null;
        Properties pros = null;
        Connection conn =null;
        try {
            is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
            pros = new Properties();
            pros.load(is);

            //2.读取配置信息
            String user = pros.getProperty("user");
            String password = pros.getProperty("password");
            String url = pros.getProperty("url");
            String driverClass = pros.getProperty("driverClass");

            //3.加载驱动
            Class.forName(driverClass);
            //4.加载配置信息
            conn=DriverManager.getConnection(url,user,password);

            System.out.println("conn = " + conn);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

        }
    }

}
